package sg.edu.nus.iss.vmcs.controller;



import sg.edu.nus.iss.vmcs.util.VMCSException;

/**
 * 
 * @author Chomeaye
 *
 */
public class CoinReceiver extends MoneyReceiver {
	//private List<Observer> observers = new ArrayList<Observer>();
	private CoinReceiverState state;
	//private Coin[] coins;
	//private CoinButtonPanel panel;
	private TransactionController transactionController;
	private double changeGivenAmount;
	private double totalInserted;
	
	
	

	public double getTotalInserted() {
		return totalInserted;
	}


	public void setTotalInserted(double totalInserted) {
		this.totalInserted = totalInserted;
	}


	public double getChangeGivenAmount() {
		return changeGivenAmount;
	}


	public void setChangeGivenAmount(double changeGivenAmount) {
		this.changeGivenAmount = changeGivenAmount;
	}


	public CoinReceiver(TransactionController transactionController)throws VMCSException{ 
		super(transactionController);
			
		this.transactionController = transactionController;
		
		if (transactionController == null)
			throw new VMCSException("CoinReceiver constructor", "transactionController is null");
		
		
		setState(CoinReceiverIdle.GetInstance());
		
	}


	
	public CoinReceiverState getState() {
		return this.state;
		

		
	}
	 
	public void setState(CoinReceiverState state) {
		this.state = state;
	}

	public void startCoinReceiver() throws VMCSException {
		state.startCoinReceiver(this);
	}
	
	public void stopCoinReceiver() throws VMCSException{
		state.stopCoinReceiver(this);
	}
	
	public void startReceive() throws VMCSException{
		state.startReceive(this);
	}
	
	public void continueReceive() throws VMCSException{
		state.continueReceive(this);
	}
	
	public boolean storeCash() throws VMCSException{
		return state.storeCash(this);
	}
	
	public void stopReceive() throws VMCSException{
		state.stopReceive(this);
	}
	
	public void refundCash() throws Exception{
		state.refundCash(this);
	}

	public void terminateFault() throws Exception{
		state.terminateFault(this);
	}

	public void receiveCoin(double totAmtInserted) throws Exception{
		double totalInserted = transactionController.getCoinInputBox().getMp().getTotalCoinsInserted();
		double brandPrice = transactionController.getDrinkSelectionBox().getCurrentlySelectedBrand().getPrice();
		
		setTotalInserted(totalInserted);
	
		if (totalInserted < 0)
		{
			transCtlr.setMoneyReceiver(this);
			startReceive();
		}
		else if(totalInserted < brandPrice)
		{
			if (!(state instanceof CoinReceiving)) {
				startReceive();
				state.receiveCoin(this,totAmtInserted);
			} else {
				state.receiveCoin(this,totAmtInserted);
			}
			
		}
		else if(totalInserted >= brandPrice)
		{
			
			setChangeGivenAmount(totalInserted - brandPrice);
					
						
			//Display Total Inserted before change
			getTransactionController().getCoinInputBox().getMp().setTotalCoinsInserted(getTotalInserted());
			getTransactionController().getCoinInputBox().getMp().notifyObservers(getTotalInserted() + " c");
			
			//TODO : ChangeGiven (to implement in completeTransaction)
			getTransactionController().completeTransaction(getChangeGivenAmount(),this);
			
			
		}
		
	}
		
	public TransactionController getTransactionController()
	{
		return transCtlr;
	}

//	@Override
//	public void setActive(boolean active) throws VMCSException {
//		// TODO Auto-generated method stub
//		
//	}
	
	
	
//	public Coin[] getCoins()
//	{
//		return this.coins;
//	}
//	
//	public void setCoins(Coin[] coins)
//	{
//		this.coins = coins;
//	}
	
	
}